{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d4e37e01-a168-4c51-8429-fed63de294e9",
   "metadata": {},
   "source": [
    "# 安装配置ChatTTS环境-备选\n",
    "\n",
    "同济子豪兄 2024-6-11"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71305069-b9b0-4cd9-a104-8759ed4995dd",
   "metadata": {},
   "source": [
    "## 删除原有的ChatTTS目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "df118633-81d6-4ad0-a520-2d6c30482d1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm -rf ChatTTS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f89ec1bc-df36-49ff-b988-1e81f42b4337",
   "metadata": {},
   "source": [
    "## 从Github下载ChatTTS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7a4059af-3c89-4787-a4af-6fc02cb78382",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cloning into 'ChatTTS'...\n",
      "remote: Enumerating objects: 174, done.\u001b[K\n",
      "remote: Counting objects: 100% (136/136), done.\u001b[K\n",
      "remote: Compressing objects: 100% (86/86), done.\u001b[K\n",
      "remote: Total 174 (delta 96), reused 52 (delta 50), pack-reused 38\u001b[K\n",
      "Receiving objects: 100% (174/174), 2.73 MiB | 3.73 MiB/s, done.\n",
      "Resolving deltas: 100% (97/97), done.\n"
     ]
    }
   ],
   "source": [
    "!git clone https://github.com/2noise/ChatTTS.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2898bd6-161d-4cb4-adea-c7f80ef74e87",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 切换到子豪兄同个版本 20240613\n",
    "# !git reset --hard 0359f28545c7285d7ec3d98ca25c1099e7890c4a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d050e2a-9b15-40a2-b332-16084ad4e8e5",
   "metadata": {},
   "source": [
    "## 进入ChatTTS主目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "38fe6aa4-ce52-4597-933f-51e5bd7f0224",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "817026fe-1ff3-4525-b57f-adfb3abb142a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/gemini/code'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "82c7f509-b017-4165-af64-c88ec928b37d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/gemini/code/ChatTTS'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.chdir('ChatTTS')\n",
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "558425d7-75b9-4dd7-8254-7329f11012e9",
   "metadata": {},
   "source": [
    "## 安装所需工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "382fd845-5bf8-4820-9883-624a5c1ba6b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b7437ab7-d64b-4b5e-a585-368fcf77404a",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install soundfile torchaudio pynini==2.1.5 nemo_text_processing gradio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "125d1591-b0fd-4d7a-bcc3-cd6c17a63baf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install -U ipywidgets huggingface_hub[hf_transfer]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c4fa475-20ee-4a7a-a874-de968f0c8a79",
   "metadata": {},
   "source": [
    "## 验证安装成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5b39b649-896e-464a-a6d5-1dd064d24741",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torchaudio\n",
    "import torch\n",
    "import soundfile\n",
    "from IPython.display import Audio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c9958017-eb66-40d1-a3ef-379b46f702ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# from ChatTTS import ChatTTS\n",
    "import ChatTTS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e256535-29dc-4ab1-ab38-20927cfe0c0a",
   "metadata": {},
   "source": [
    "如果导入上述工具包没有报错，则说明安装配置成功！\n",
    "\n",
    "如果报错，则换成运行被注释掉的那行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b79af99-9675-43d4-b798-7b58fa04c674",
   "metadata": {},
   "source": [
    "## 修改代码\n",
    "\n",
    "- 第一处\n",
    "\n",
    "将`ChatTTS/core.py`的143行到149行注释掉，效果如下：\n",
    "\n",
    "```python\n",
    "        # if do_text_normalization:\n",
    "        #     for i, t in enumerate(text):\n",
    "        #         _lang = detect_language(t) if lang is None else lang\n",
    "        #         self.init_normalizer(_lang)\n",
    "        #         text[i] = self.normalizer[_lang](t)\n",
    "        #         if _lang == 'zh':\n",
    "        #             text[i] = apply_half2full_map(text[i])\n",
    "```\n",
    "\n",
    "- 第二处\n",
    "\n",
    "将`ChatTTS/core.py`的55行，添加`endpoint='https://hf-mirror.com'`，效果如下：\n",
    "```python\n",
    "download_path = snapshot_download(repo_id=\"2Noise/ChatTTS\", allow_patterns=[\"*.pt\", \"*.yaml\"], endpoint='https://hf-mirror.com')\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dee9e371-687e-4c78-b3b6-b90a982c89d4",
   "metadata": {},
   "source": [
    "## 从Hugging Face下载预训练模型（方法一）\n",
    "\n",
    "在命令行中运行\n",
    "\n",
    "```python\n",
    "\n",
    "python\n",
    "\n",
    "from ChatTTS import ChatTTS\n",
    "\n",
    "# import ChatTTS\n",
    "\n",
    "chat = ChatTTS.Chat()\n",
    "\n",
    "chat.load_models(force_redownload=True)\n",
    "\n",
    "```\n",
    "\n",
    "如果下载卡住，重新运行即可"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80ef4a87-935c-4383-9419-6f68239f67c4",
   "metadata": {},
   "source": [
    "## 从Hugging Face下载预训练模型（方法二）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0e99f752-9d99-40b9-a9ed-e705ebb3dd26",
   "metadata": {},
   "outputs": [],
   "source": [
    "chat = ChatTTS.Chat()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "472dec99-60ca-4ca5-8ce7-9258b2228ba8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:ChatTTS.ChatTTS.core:Download from HF: https://huggingface.co/2Noise/ChatTTS\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6dde73e677654739a140fcbc9fb42e97",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Fetching 11 files:   0%|          | 0/11 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ab67e14e849b4bb5b866020527169a0d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "GPT.pt:   0%|          | 0.00/901M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f32b6be723c04c00866fdbb67938c69e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "config/decoder.yaml:   0%|          | 0.00/117 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6f03e5b28d62478a81ea683a681e8e78",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Decoder.pt:   0%|          | 0.00/104M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d43c2d4032f54b199cd9b323a2c2ff7c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "spk_stat.pt:   0%|          | 0.00/4.26k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7f4e59e306334194bc694146f9b67c46",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Vocos.pt:   0%|          | 0.00/54.4M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "74de9a69fa074a368cdf41dcf074e16a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "config/dvae.yaml:   0%|          | 0.00/143 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4b52af7da05247b5b4a8703eb0d0d5f4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "DVAE.pt:   0%|          | 0.00/27.7M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a3537cbd18e8492aaf63abe395d4eea9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "config/gpt.yaml:   0%|          | 0.00/346 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "42cb99878c4f49feb810bc130d037bfa",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "config/path.yaml:   0%|          | 0.00/309 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dc37527e6f504acb837f156ac7304adb",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "config/vocos.yaml:   0%|          | 0.00/460 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "chat.load_models(force_redownload=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93010b54-2a97-4ecf-b19b-4376983e0761",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
